#ifndef MODULEHPLCDETECTORDEFINE_H
#define MODULEHPLCDETECTORDEFINE_H

#include <QVector>
#include "modulehplcdefine.h"

namespace hplc
{

struct AuPair
{
    double wave;
    double wave2;
};

struct RefSamPair
{
    double ref;
    double sam;
};

enum OpticalGratingType
{
    OpticalGratingType_Plane = 0, // 平面光栅
    OpticalGratingType_Concave, // 凹面光栅
};

struct DetectorMechanicalParam
{
    quint16             fullstepsPerRound; // 电机转一圈需要多少全步
    double              reductionGearRatio; // 减速比

    OpticalGratingType  opticalGratingType; // 光栅类型
    double              opticalGratingFactor; // 光栅因数
};

enum HomeWave
{
    HomeWave_ZeroLevel = 0, // 0级
    HomeWave_656nm,
    HomeWave_ZeroLevel_656nm, // 先粗略找0级，再细致找656nm
};

struct DetectorConfigParam
{
    DriverType		driverType; // 驱动器类型

    HomeWave        homeWave; // 找原点时的参考波长

    double          waveMin; // 最小波长
    double          waveMax; // 最大波长
};

enum DetectorModel
{
    DetectorModel_SingleWave = 0, // 单波长
    DetectorModel_DoubleWave, // 双波长
    DetectorModel_TripleWave, // 三波长
};

struct DetectorDeviceParam
{
    DetectorModel   model; // 型号

    ComType         comType; // 通信端口
    quint8          com; // 串口号
    NetType         netType; // 网口通信方式
    QString         remoteIP;
    quint16         remotePort;
    QString         localIP;
    quint16         localPort;

    quint8          isConnect;
    quint8          isDisconncetException; // 连接过程中异常断开连接，则重新连接。主要用于TCP Clinet
};

enum DetecorWorkMode
{
    DetectorWorkMode_Normal = 0,
    DetectorWorkMode_WaveCalib = 0x01,
    DetectorWorkMode_DACCalib = 0x02,
};

struct DetectorWaveCalibData
{
    double			position;
    double			wave;

    bool operator==(const DetectorWaveCalibData &other) const
    {
        return (qFuzzyCompare(position, other.position) && qFuzzyCompare(wave, other.wave));
    }
};

enum TimeConst
{
    TimeConst_0_1 = 0,
    TimeConst_0_2,
    TimeConst_0_5,
    TimeConst_1,
    TimeConst_2,
    TimeConst_5,
};

enum LampType
{
    LampType_Deuterium = 0, // 氘灯
    LampType_Tungsten, // 钨灯
};

enum Channel
{
    Channel_Single = 0, // 单通道
    Channel_Double, // 双通道
};

enum Range
{
    Range_0_0001 = 0,
    Range_0_0002,
    Range_0_0005,
    Range_0_001,
    Range_0_002,
    Range_0_005,
    Range_0_01,
    Range_0_02,
    Range_0_05,
    Range_0_1,
    Range_0_2,
    Range_0_5,
    Range_1,
    Range_2,
    Range_5,
    Range_10,
};

struct ScanWaveParam
{
    quint16     startWave;
    quint16     endWave;
    quint8      speed;
};

struct WaveParam
{
    double      wave; // 波长 nm
    double      au; // 吸光度
    quint32     ref; // 参比路能量值
    quint32     sam; // 样品路能力值
};

struct ScanWaveStatus
{
    quint16 	wave;
    double 		au;
};

struct DetectorBasicParam
{
    // 波长
    WaveParam       wave;
    WaveParam       wave2;
    WaveParam       wave3;

    bool            isLampOn;

    // 时间常数
    TimeConst       timeConst;

    // 量程
    Range           range;

    // 通道
    Channel         channel;

    // 灯类型
    LampType        lampType;

    // 波长扫描参数
    ScanWaveParam   scanWaveParam;
    ScanWaveStatus	scanWaveStatus;

    bool 			isHome;
    bool			isStart;
    bool			isPause;
};

enum UploadObject
{
    UploadObject_Au = 0,
    UploadObject_Ref,
    UploadObject_Sam,
};

enum IntegralRange
{
    IntegralRange_ext = 0,
    IntegralRange_50pC,
    IntegralRange_100pC,
    IntegralRange_150pC,
    IntegralRange_200pC,
    IntegralRange_250pC,
    IntegralRange_300pC,
    IntegralRange_350pC,
};

struct IntegralParam
{
    quint16         time; // 积分时间
    IntegralRange   range; // 积分量程
};

struct DetectorSystemParam
{
    quint8       							workMode; // ref enum DetectorWorkMode

    QList<DetectorWaveCalibData>			waveCalibTable;
    qint32                                  homeOffset; // 每次回原点后，需要偏移的步数
    quint32                                 waveOffset; // 每次方向改变时，需要多走的步数(波长定位用，这样做是为了减小回程误差)

    double                                  dacZeroVoltage; // DAC输出的零点电压

    IntegralParam                           integralParam; // 积分参数(正常工作时使用)
    IntegralParam                           homeIntegralParam; // 回原点时的积分参数(只在回原点时使用)

    quint8			                        auUploadFrequency; // 上传频率 0:不上传，1:50ms上传一次，2:100ms上传一次
    UploadObject                            uploadObject; // 上传对象

    quint32 								lampUsedTime;
    quint16									lampOnOffTimes;
    quint8									lampCount;
    bool									coil;
    double									auFactor;

    qint32 									rasterStep;
};

// 检测器的所有参数
struct DetectorParam : public CommonParam
{
    DetectorMechanicalParam			mechanicalParam;
    DetectorConfigParam				configParam;

    DetectorDeviceParam				deviceParam;
    DetectorBasicParam				basicParam;
    DetectorSystemParam				systemParam;
};

} // namespace hplc

Q_DECLARE_METATYPE(hplc::AuPair)
Q_DECLARE_METATYPE(hplc::RefSamPair)

Q_DECLARE_METATYPE(hplc::Range)
Q_DECLARE_METATYPE(hplc::TimeConst)
Q_DECLARE_METATYPE(hplc::Channel)
Q_DECLARE_METATYPE(hplc::LampType)
Q_DECLARE_METATYPE(hplc::DetectorModel)
Q_DECLARE_METATYPE(hplc::WaveParam)
Q_DECLARE_METATYPE(hplc::ScanWaveParam)
Q_DECLARE_METATYPE(hplc::ScanWaveStatus)
Q_DECLARE_METATYPE(hplc::DetectorWaveCalibData)
Q_DECLARE_METATYPE(QList<hplc::DetectorWaveCalibData>)
Q_DECLARE_METATYPE(hplc::IntegralRange)
Q_DECLARE_METATYPE(hplc::IntegralParam)
Q_DECLARE_METATYPE(hplc::UploadObject)

Q_DECLARE_METATYPE(hplc::DetectorMechanicalParam)
Q_DECLARE_METATYPE(hplc::DetectorConfigParam)
Q_DECLARE_METATYPE(hplc::DetectorDeviceParam)
Q_DECLARE_METATYPE(hplc::DetectorBasicParam)
Q_DECLARE_METATYPE(hplc::DetectorSystemParam)

Q_DECLARE_METATYPE(hplc::DetectorParam)

#endif // MODULEHPLCDETECTORDEFINE_H
